#include "UIMain.h"
#include "UIPause.h"
#include "GameScene.h"
#include "GameLayer.h"
UIMain::UIMain()
{

}

UIMain::~UIMain()
{

}

UIMain* UIMain::create()
{
	UIMain* p = new UIMain();
	if (p && p->init())
	{
		p->autorelease();
		return p;
	}
	CC_SAFE_DELETE(p);
	return NULL;
}

bool UIMain::init()
{
	CCLayer::init();

	bool bRet = false;
	do 
	{
		UILayer* pUILayer = UILayer::create();
		CC_BREAK_IF(!pUILayer);

		UIPanel* pRoot = (UIPanel*)GUIReader::shareReader()->widgetFromJsonFile("ui/main/main_1.json");
		CC_BREAK_IF(!pRoot);
		pRoot->setSize(pUILayer->getContentSize());
		pUILayer->addWidget(pRoot);
		addChild(pUILayer);

		m_pScore = (UILabelAtlas*)UIHelper::seekWidgetByTag(pRoot, UIMainTag_score);

		UIButton* pbtn = (UIButton*)UIHelper::seekWidgetByTag(pRoot, UIMainTag_pause);
		pbtn->addTouchEventListener(this, toucheventselector(UIMain::onPause));

		bRet = true;
	} while (0);

	return bRet;
}

void UIMain::setScore(int score)
{
	m_pScore->setStringValue(CCString::createWithFormat("%d", score)->getCString());
}

void UIMain::onPause(CCObject* pSender, TouchEventType eType)
{
	if (eType != TouchEventType::TOUCH_EVENT_ENDED)
	{
		return;
	}

	UIPause* pUIPause = UIPause::create();
	getParent()->addChild(pUIPause);

	GameScene* pScene = (GameScene*)getParent();
	pScene->getGameLayer()->pauseSchedulerAndActions();
}

